/*
 *  $Id: SoundEvent.cs 1199 2010-11-20 17:38:24Z thenn.erannor $
 *	This file is a part of Ragima CCG.
 *	(C) 2008-2010 Ragima development group.
 *
 *	Licensed under the terms of the GNU GPL License version 2.
 *
 */

using System.Collections.Generic;
using System.IO;

using AgateLib.AudioLib;

using Nereal.Extensions;
using Nereal.Serialization;

namespace Ragima {
    /// <summary>
    /// Список звуковых событий.
    /// </summary>
    [XmlRootName("events")]
    public sealed class SoundEventList {
        private Dictionary<string, SoundEvent> _events;

        public SoundEventList() {
            _events = new Dictionary<string, SoundEvent>();
        }

        public static SoundEventList Load(string filename) {
            var path = Paths.Data[filename];
            return path.Exists ? Serializer.Default.Deserialize<SoundEventList>(path) : new SoundEventList();
        }

        [XmlName(null, "event"), KeyName("id")]
        public Dictionary<string, SoundEvent> Events {
            get { return _events; }
        }

        public int Count {
            get {
                int count = 0;
                foreach (var ev in _events.Values)
                    count += ev.Count;
                return count;
            }
        }

        public void Load(SimpleEvent onLoad) {
            foreach (var ev in _events.Values)
                ev.Load(onLoad);
        }

        public void Play(string id) {
            var ev = _events.GetOrDefault(id);
            if (ev != null)
                ev.Play();
        }
        public void Play(string id, int index) {
            var ev = _events.GetOrDefault(id);
            if (ev != null)
                ev.Play(index);
        }
    }

    /// <summary>
    /// Звуковое событие.
    /// Содержит список звуков, из которых при событии проигрывается один случайный.
    /// </summary>
    [XmlRootName("event")]
    public sealed class SoundEvent {
        private List<SoundEventFile> _sounds;

        public SoundEvent() {
            _sounds = new List<SoundEventFile>();
        }

        [XmlName(null, "sound")]
        public List<SoundEventFile> Sounds {
            get { return _sounds; }
        }

        public int Count {
            get { return _sounds.Count; }
        }

        public void Load(SimpleEvent onLoad) {
            foreach (var sound in _sounds)
                sound.Load(onLoad);
        }

        public void Play() {
            if (_sounds.Count == 0)
                return;
            int index = 0;
            if (_sounds.Count > 1)
                index = Randomizer.Global.Next(_sounds.Count);
            Play(index);
        }
        public void Play(int index) {
            var sound = _sounds.GetOrDefault(index);
            if (sound != null)
                sound.Play();
        }
    }

    /// <summary>
    /// Отдельный звуковой файл для события.
    /// </summary>
    [XmlRootName("sound"), ConstructorParameters("Name")]
    public sealed class SoundEventFile {
        private string _name;
        private SoundBuffer _sound;

        public SoundEventFile(string name) {
            _name = name;
            _sound = null;
        }

        [XmlName("name")]
        public string Name {
            get { return _name; }
        }

        public void Load(SimpleEvent onLoad) {
            _sound = Application.Config.Sound.DriverEnabled ? new SoundBuffer(_name) : null;
            if (onLoad != null)
                onLoad();
        }

        public void Play() {
            OldScene.Play(_sound);
        }
    }
}
